package com.example.kaogong.sys.controller;


import com.example.kaogong.common.exception.NotExistException;
import com.example.kaogong.common.exception.ParamsErrorException;
import com.example.kaogong.common.utils.Assert;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.dto.LoginDto;
import com.example.kaogong.sys.pojo.dto.RegisterDto;
import com.example.kaogong.sys.pojo.dto.ResetPasswordDto;
import com.example.kaogong.sys.pojo.dto.UserInfoUpdateDto;
import com.example.kaogong.sys.pojo.vo.UserInfoVo;
import com.example.kaogong.sys.service.IUserService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
import java.util.Map;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author    
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "用户登录并返回token", notes = "Authorization：Bearer token")
    @PostMapping(value = "/login")
    public RespBean login(@Valid @RequestBody LoginDto loginDto) {
        log.info("request:{}", new Gson().toJson(loginDto));
        RespBean respBean = userService.login(loginDto.getUsername(), loginDto.getPassword());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "修改当前用户密码", notes = "旧密码不能与新密码相同，旧密码必须与当前密码匹配")
    @PutMapping(value = "/password/reset")
    public RespBean resetPassword(@RequestBody ResetPasswordDto passwordDto, Principal principal) {
        if (principal == null)
            return RespBean.error();
        log.info("request:{}", new Gson().toJson(passwordDto));
        RespBean respBean = userService.resetPassword(passwordDto, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "获取当前登录用户基本信息")
    @GetMapping(value = "/info/individual")
    public RespBean<UserInfoVo> getCurrentUserInfo(Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notBlank(principal.getName(), new ParamsErrorException("用户名不能为空"));
        log.info("username:{}", principal.getName());
        RespBean<UserInfoVo> respBean = userService.getUserInfo(principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据用户名获取用户基本信息")
    @GetMapping("/info/{username}")
    public RespBean<UserInfoVo> getUserInfoByUsername(@PathVariable String username) {
        Assert.notBlank(username, new ParamsErrorException("用户名不能为空"));
        log.info("username:{}", username);
        RespBean<UserInfoVo> respBean = userService.getUserInfo(username);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "修改当前登录用户基本信息", notes = "管理员没有个人基本信息")
    @PutMapping(value = "/info/individual")
    public RespBean updateCurrentUserInfo(@RequestBody UserInfoUpdateDto userInfoUpdateDto, Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notBlank(principal.getName(), new ParamsErrorException("用户不存在"));
        log.info("request:{}", new Gson().toJson(userInfoUpdateDto));
        RespBean respBean = userService.updateCurrentUserInfo(userInfoUpdateDto, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "修改用户头像", notes = "文件名为 /avatar/#{uid}.jpg")
    @PutMapping(value = "/info/avatar/{fileName}")
    public RespBean updateUserAvatar(@PathVariable String fileName, Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notBlank(principal.getName(), new ParamsErrorException("用户不存在"));
        Assert.notBlank(fileName, new ParamsErrorException("用户头像文件名不能为空"));
        log.info("fileName:{}", fileName);
        RespBean respBean = userService.updateUserAvatar(fileName, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "判断用户名是否存在", notes = "用户名是登录凭证，必须唯一")
    @GetMapping(value = "/info/exist/{username}")
    public RespBean judgeUsernameExist(@PathVariable String username) {
        Assert.notBlank(username, new ParamsErrorException("用户名不能为空"));
        log.info("username:{}", username);
        RespBean respBean = userService.judgeUsernameExist(username);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "用户注册", notes = "1.学生注册和教师添加为同一接口,2.用户名不能重复")
    @PostMapping(value = "/register")
    public RespBean userRegister(@Valid @RequestBody RegisterDto registerDto) {
        log.info("request:{}", new Gson().toJson(registerDto));
        RespBean respBean = userService.userRegister(registerDto);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }
}
